<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo布局 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单登录区域 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../../api/login";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      // 表单重置
      this.$refs.loginForm.resetFields();
    },
    // 登录
    login() {
      this.$refs.loginForm.validate(async (valid) => {
        // console.log(valid);
        //  if(valid){
        //    login(this.loginForm.username,this.loginForm.password).then((res)=>{
        //      console.log(res)
        //    })
        //  }
         
        //  es6的.then()方法
        // if(valid){
        //   login(this.loginForm)
        //   .then(res=>{
        //     console.log(res)
        //   })
        // }

        if (!valid) return;
        var { data: res } = await login(this.loginForm);
        // console.log(res);
        if (res.meta.status != 200) {
          this.$message.error('登录失败')
        } else {
          console.log("登录成功");
          this.$message({
            message:'登录成功',
            type:'success'
          })
          // 登录成功后保存session，然后跳转路由
         var {token} = res.data
         window.sessionStorage.setItem('token',token)
         this.$router.push('/home')
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100vh;
  position: relative;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-sizing: border-box;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btn {
  display: flex;
  justify-content: flex-end;
}
</style>